<template>
	<view style="padding: 40rpx;">
		<u-form :model="userInfo" :rules="rules" ref="uForm" :errorType="errorType">
			<u-form-item label="头像" prop="userInfo.avatar" borderBottom>
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="1"></u-upload>
			</u-form-item>
			<u-form-item label="姓名" borderBottom>
				<u--input clearable v-model="userName" :disabled="true"></u--input>
			</u-form-item>
			<u-form-item label="昵称" borderBottom>
				<u--input placeholder="请输入昵称" clearable v-model="userInfo.nickName"></u--input>
			</u-form-item>
			<u-form-item label="地址" borderBottom>
				<u--input placeholder="请输入你的地址" clearable v-model="userInfo.address"></u--input>
			</u-form-item>
			<u-form-item label="电话" prop="mobile" borderBottom>
				<u--input type="number" placeholder="请输入电话" clearable v-model="userInfo.mobile"></u--input>
			</u-form-item>
			<u-form-item>
				<u-button style="margin-top:15rpx ;" type="primary" @click="EditUserInfoHandler">修改</u-button>
			</u-form-item>
		</u-form>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		GetUserInfoAPI,
		EditUserInfoAPI
	} from "../../services/api.js"
	import {

		dalImg
	} from "../../utils/tools.js"
	export default {
		data() {
			return {
				userName: "",
				errorType: 'message',
				mobileState: false,
				fileList1: [],
				userInfo: {
					nickName: "",
					avatar: "",
					mobile: 0,
					address: ""
				},
				list: {
					type: '',
					title: '',
					message: "",
				},
				rules: {
					mobile: [{
							required: true,
							message: '请输入手机号',
							trigger: ['blur',"change"],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return this.mobileState = uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['blur',"change"],
						}
					]
				},
			}
		},
		methods: {
			showToast(params) {
				this.$refs.uToast.show({
					...params,
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://101.42.35.238:3006/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							// console.log(JSON.parse(res.data).data);
							this.userInfo.avatar = JSON.parse(res.data).data
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			getUserInfo() {
				GetUserInfoAPI().then((res) => {
					console.log(res.data);
					this.userName = res.data.userName
					for (let i in this.userInfo) {
						this.userInfo[i] = res.data[i]
					}
					this.fileList1 = [{
						url: dalImg(res.data.avatar)
					}]
					console.log(this.userInfo);
				})
			},
			EditUserInfoHandler() {
				if (this.mobileState) {
					EditUserInfoAPI(this.userInfo).then((res) => {
						console.log(res);
						if (res.success) {
							this.list.message = "修改成功！"
							this.list.type = "success"
							this.showToast(this.list)
							setTimeout(() => {
								uni.switchTab({
									url: "/pages/my/my"
								})
							}, 500)
						}
						this.getUserInfo()
					})
				}
			}

		},
		onLoad() {
			this.getUserInfo()
		},
		onShow() {
		if (!gettoken()) {
			this.isShow = true
			uni.navigateTo({
				url: "/pages/login/login"
			})
		}
		}
	}
</script>

<style>

</style>